package com.qxsx.midi.activity.home;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.exam.ClassVideoDetailsActivity;
import com.qxsx.midi.activity.exam.ClassVideoListActivity;
import com.qxsx.midi.activity.main.DownLodeActivity;
import com.qxsx.midi.activity.me.LoginActivity;
import com.qxsx.midi.adapter.BuyAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.DataManager;
import com.qxsx.midi.db.MyMusicFileDBTool;
import com.qxsx.midi.model.Buy;
import com.qxsx.midi.model.ClassVideo;
import com.qxsx.midi.model.MyMusicFile;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.XListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Zrj on 2015/9/6.
 */
public class BuyListActivity extends Activity implements View.OnClickListener, XListView.IXListViewListener {
    private CustomProgress customProgress;
    private XListView lv_buy;
    private List<Buy> list;
    private int pageNum = 1;//初始化页号为1
    private BuyAdapter buyAdapter;
    //返回按钮
    private LinearLayout Ll_back;
    private LinearLayout ll_pop;
    //筛选
    private CheckBox header_right_btn;


    //执行下拉、上拉线程
    private Handler mHandler;

    String major_id = "";
    String type = "";
    String grade_num = "";


    public static final int WHAT_INITDATA = 100;
    public static final int WHAT_TIAOZHUAN = 200;

    /**
     * 专业中的组件
     */
    //电吉他，爵士鼓，电贝司，音乐基础，不限
    private Button btn_guitar, btn_agitate, btn_bass, btn_basic, btn_major_nolimt;

    /**
     * 曲谱/教程
     */
    private Button btn_mucou, btn_course, btn_music;

    /**
     * 等级
     */
    private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven, btn_eight, btn_nine, btn_grade_nolimt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_list);
        customProgress = CustomProgress.show(BuyListActivity.this, "请稍后....", true, null);
        findById();


        lv_buy.setPullLoadEnable(true);
        lv_buy.setPullRefreshEnable(true);
        mHandler = new Handler();
        lv_buy.setXListViewListener(this);

    }


    /**
     * 定义一个Handle接收消息
     */
    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case WHAT_INITDATA:
                    InitData();
                    break;

                case WHAT_TIAOZHUAN:
                    startActivity(new Intent(BuyListActivity.this, LoginActivity.class));
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };


    private void InitData() {
        list = new ArrayList<>();

        buyAdapter = new BuyAdapter(AppContext.getContext(), list, BuyListActivity.this, handler);
        lv_buy.setAdapter(buyAdapter);

        buylist(1);
    }

    @Override
    protected void onResume() {
        super.onResume();
        InitData();


    }

    private void findById() {
        //专业
        btn_guitar = (Button) findViewById(R.id.btn_guitar);
        btn_guitar.setOnClickListener(this);

        btn_agitate = (Button) findViewById(R.id.btn_agitate);
        btn_agitate.setOnClickListener(this);

        btn_bass = (Button) findViewById(R.id.btn_bass);
        btn_bass.setOnClickListener(this);

        btn_basic = (Button) findViewById(R.id.btn_basic);
        btn_basic.setOnClickListener(this);

        btn_major_nolimt = (Button) findViewById(R.id.btn_major_nolimt);
        btn_major_nolimt.setOnClickListener(this);
        btn_major_nolimt.setSelected(true);


        //等级
        btn_one = (Button) findViewById(R.id.btn_one);
        btn_one.setOnClickListener(this);

        btn_two = (Button) findViewById(R.id.btn_two);
        btn_two.setOnClickListener(this);

        btn_three = (Button) findViewById(R.id.btn_three);
        btn_four = (Button) findViewById(R.id.btn_four);
        btn_five = (Button) findViewById(R.id.btn_five);
        btn_six = (Button) findViewById(R.id.btn_six);
        btn_seven = (Button) findViewById(R.id.btn_seven);
        btn_eight = (Button) findViewById(R.id.btn_eight);
        btn_nine = (Button) findViewById(R.id.btn_nine);

        btn_three.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_five.setOnClickListener(this);
        btn_six.setOnClickListener(this);
        btn_seven.setOnClickListener(this);
        btn_eight.setOnClickListener(this);
        btn_nine.setOnClickListener(this);

        btn_grade_nolimt = (Button) findViewById(R.id.btn_grade_nolimt);
        btn_grade_nolimt.setOnClickListener(this);
        btn_grade_nolimt.setSelected(true);


        //曲谱/教程

        btn_mucou = (Button) findViewById(R.id.btn_mucou);
        btn_course = (Button) findViewById(R.id.btn_course);
        btn_music = (Button) findViewById(R.id.btn_music);
        btn_mucou.setOnClickListener(this);
        btn_course.setOnClickListener(this);
        btn_music.setOnClickListener(this);
        btn_mucou.setSelected(true);


        ll_pop = (LinearLayout) findViewById(R.id.ll_pop);
        ll_pop.setFocusable(true);
        ll_pop.setClickable(true);
        lv_buy = (XListView) findViewById(R.id.lv_buy);
        Ll_back = (LinearLayout) findViewById(R.id.header_left_ll);
        Ll_back.setOnClickListener(this);
        header_right_btn = (CheckBox) findViewById(R.id.header_right_btn);
        header_right_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ll_pop.setVisibility(View.VISIBLE);
                    lv_buy.setFocusable(false);
                    ll_pop.setFocusable(true);
                } else {
                    InitData();
                    ll_pop.setVisibility(View.GONE);
                    lv_buy.setFocusable(true);

                }
            }
        });


    }


    private void buylist(int pageNum) {
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        major_id = (String) SPUtils.get(AppContext.getContext(),"apply_major_id","");
        try {
            object.put("major_id", major_id);
            object.put("type", type);
            object.put("grade_num", grade_num);
            object.put("nowpage", pageNum + "");
            object.put("pagenum", "10");
            object.put("user_id", user_id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("401", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("code", message + "");
                Log.w("code", array + "");

                if (array == null) {
                    return;
                }
                try {
                    list = new ArrayList<Buy>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        Buy buy = new Buy();
                        buy.setBuy_name(object1.getString("name"));
                        buy.setBuy_tapy(object1.getString("type"));
                        buy.setBuy_id(object1.getString("id"));
                        buy.setBuy_url(object1.getString("file_url"));
                        buy.setBuy_buy(object1.getString("buy"));
                        //buy.setBuy_img(object1.getString("icon-img"));
                        String major_id = object1.getString("major_id");

                        if (major_id.toString().equals("2172405d-17bb-5f40-da92-cf9cc285a980")) {
                            buy.setBuy_img(R.mipmap.exam_wait_guitar);
                        } else if (major_id.toString().equals("cd12a552-95ee-9d9b-1efb-8c302c6b52c2")) {
                            buy.setBuy_img(R.mipmap.exam_wait_jazz);
                        } else if (major_id.toString().equals("5bb9bcb5-88a0-9c85-0a15-39b51c1142be")) {
                            buy.setBuy_img(R.mipmap.exam_wait_bass);
                        } else if (major_id.toString().equals("87b65e6a-72fc-4617-d61b-351a7d080ed7")) {
                            buy.setBuy_img(R.mipmap.exam_wait_base);
                        }

                        buy.setBuy_price(object1.getString("price"));
                        list.add(buy);
                    }
                    buyAdapter.removeAll();
                    buyAdapter.addData(list);
                    customProgress.dismiss();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }


    /**
     * 上啦加载
     */
    private void LoadMore() {
        pageNum++;
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        major_id = (String) SPUtils.get(AppContext.getContext(),"apply_major_id","");
        try {
            object.put("major_id", major_id);
            object.put("type", type);
            object.put("grade_num", grade_num);
            object.put("nowpage", pageNum + "");
            object.put("pagenum", "10");
            object.put("user_id", user_id);
            array.put(object);
            Log.w("code", array + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("401", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("code", message + "");
                Log.w("code", array + "");

                //JSONObject object1 = array.getJSONObject(i);
                if (array == null) {
                    return;
                }
                try {
                    lv_buy.stopLoadMore();
                    list = new ArrayList<Buy>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        Buy buy = new Buy();
                        buy.setBuy_name(object1.getString("name"));
                        buy.setBuy_tapy(object1.getString("type"));
                        buy.setBuy_id(object1.getString("id"));
                        buy.setBuy_url(object1.getString("file_url"));
                        buy.setBuy_buy(object1.getString("buy"));
                        String major_id = object1.getString("major_id");
                        if (major_id.toString().equals("2172405d-17bb-5f40-da92-cf9cc285a980")) {
                            buy.setBuy_img(R.mipmap.exam_wait_guitar);
                        } else if (major_id.toString().equals("cd12a552-95ee-9d9b-1efb-8c302c6b52c2")) {
                            buy.setBuy_img(R.mipmap.exam_wait_jazz);
                        } else if (major_id.toString().equals("5bb9bcb5-88a0-9c85-0a15-39b51c1142be")) {
                            buy.setBuy_img(R.mipmap.exam_wait_bass);
                        } else if (major_id.toString().equals("87b65e6a-72fc-4617-d61b-351a7d080ed7")) {
                            buy.setBuy_img(R.mipmap.exam_wait_base);
                        }
                        buy.setBuy_price(object1.getString("price"));

                        list.add(buy);

                    }
                    buyAdapter.addData(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    /**
     * 下拉刷新
     */
    private void LoadRefresh() {
        pageNum = 1;
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        major_id = (String) SPUtils.get(AppContext.getContext(),"apply_major_id","");
        try {
            object.put("major_id", major_id);
            object.put("type", type);
            object.put("grade_num", grade_num);
            object.put("nowpage", 1 + "");
            object.put("pagenum", "10");
            object.put("user_id", user_id);
            array.put(object);
            Log.w("code", array + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("401", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("code", code + "");
                Log.w("code", message + "");
                Log.w("code", array + "");

                //JSONObject object1 = array.getJSONObject(i);
                if (array == null) {
                    return;
                }
                try {
                    lv_buy.stopRefresh();
                    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
                    lv_buy.setRefreshTime(df.format(new Date()));
                    list = new ArrayList<Buy>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object1 = array.getJSONObject(i);
                        Buy buy = new Buy();
                        buy.setBuy_name(object1.getString("name"));
                        buy.setBuy_tapy(object1.getString("type"));
                        buy.setBuy_id(object1.getString("id"));
                        buy.setBuy_url(object1.getString("file_url"));
                        buy.setBuy_buy(object1.getString("buy"));
                        String major_id = object1.getString("major_id");
                        if (major_id.toString().equals("2172405d-17bb-5f40-da92-cf9cc285a980")) {
                            buy.setBuy_img(R.mipmap.exam_wait_guitar);
                        } else if (major_id.toString().equals("cd12a552-95ee-9d9b-1efb-8c302c6b52c2")) {
                            buy.setBuy_img(R.mipmap.exam_wait_jazz);
                        } else if (major_id.toString().equals("5bb9bcb5-88a0-9c85-0a15-39b51c1142be")) {
                            buy.setBuy_img(R.mipmap.exam_wait_bass);
                        } else if (major_id.toString().equals("87b65e6a-72fc-4617-d61b-351a7d080ed7")) {
                            buy.setBuy_img(R.mipmap.exam_wait_base);
                        }
                        buy.setBuy_price(object1.getString("price"));

                        list.add(buy);
                    }
                    buyAdapter.removeAll();
                    buyAdapter.addData(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }


    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadRefresh();
            }
        }, 2000);
    }

    /**
     * 上拉加载更多
     */
    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadMore();
            }
        }, 2000);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.header_left_ll:
                finish();
                break;

            //专业Button btn_guitar, btn_agitate, btn_bass, btn_basic, btn_major_nolimt;
            case R.id.btn_guitar:
                major_id = "2172405d-17bb-5f40-da92-cf9cc285a980";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(true);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(false);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_agitate:
                major_id = "cd12a552-95ee-9d9b-1efb-8c302c6b52c2";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(true);
                btn_bass.setSelected(false);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_bass:
                major_id = "5bb9bcb5-88a0-9c85-0a15-39b51c1142be";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(true);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_basic:
                major_id = "87b65e6a-72fc-4617-d61b-351a7d080ed7";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(false);
                btn_basic.setSelected(true);
                btn_major_nolimt.setSelected(false);
                break;
            case R.id.btn_major_nolimt:
                major_id = "";
                SPUtils.put(AppContext.getContext(), "apply_major_id", major_id);
                btn_guitar.setSelected(false);
                btn_agitate.setSelected(false);
                btn_bass.setSelected(false);
                btn_basic.setSelected(false);
                btn_major_nolimt.setSelected(true);
                break;


            //曲谱/教程

            case R.id.btn_music:
                type = "1";
                btn_music.setSelected(true);
                btn_course.setSelected(false);
                btn_mucou.setSelected(false);

                break;
            case R.id.btn_course:
                type = "2";
                btn_music.setSelected(false);
                btn_course.setSelected(true);
                btn_mucou.setSelected(false);

                break;
            case R.id.btn_mucou:
                type = "";
                btn_music.setSelected(false);
                btn_course.setSelected(false);
                btn_mucou.setSelected(true);

                break;


            //等级
            case R.id.btn_one:
                grade_num = "1";
                btn_one.setSelected(true);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_two:
                grade_num = "2";
                btn_one.setSelected(false);
                btn_two.setSelected(true);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_three:
                grade_num = "3";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(true);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_four:
                grade_num = "4";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(true);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_five:
                grade_num = "5";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(true);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_six:
                grade_num = "6";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(true);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_seven:
                grade_num = "7";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(true);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_eight:
                grade_num = "8";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(true);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_nine:
                grade_num = "9";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(true);
                btn_grade_nolimt.setSelected(false);

                break;
            case R.id.btn_grade_nolimt:
                grade_num = "";
                btn_one.setSelected(false);
                btn_two.setSelected(false);
                btn_three.setSelected(false);
                btn_four.setSelected(false);
                btn_five.setSelected(false);
                btn_six.setSelected(false);
                btn_seven.setSelected(false);
                btn_eight.setSelected(false);
                btn_nine.setSelected(false);
                btn_grade_nolimt.setSelected(true);

                break;


        }
    }
}
